app.controller("chargeBillCtrl", function ($scope,$interval, $http, chargeBillService) {

    //----------分页参数------------
    $scope.pageRowSize = 10;
    $scope.curPage = 1;

    //-----------------------------

    $scope.object = {};
    $scope.formMode = false;
    $scope.errors = {};

    //--------------初始化菜单---------------
    $scope.initMenu = function () {
        showSecondMenu('billingManage');
        selectSecondMenu('chargeBillQuery');
    }
    $scope.initMenu();

    //---------------错误信息--------------
    $scope.requestError = function () {
        $scope.errorMessage = "unknown error!"
    };

    $scope.init = function () {
        $scope.startDate = getDateFunction(0);  //开始日期
        $scope.endDate = getDateFunction(0);    //结束日期
        $scope.chargeBillVO={ruleRate:20};
    };

    //查询账单
    $scope.findChargeBill = function () {
        if(NullOrUndefined($("#startDate").val())){
            promptMsg("请选择开始日期！");
            return false;
        }
        if(NullOrUndefined($("#endDate").val())){
            promptMsg("请选择结束日期！");
            return false;
        }
        if(NullOrUndefined($("#contractUserSel").val())){
            promptMsg("请选择合同用户！");
            return false;
        }
        //开始日期和结束日期间隔最多3个月
        var flag = check3MonthsFunction($("#startDate").val(),$("#endDate").val());
        if(!flag){
            promptMsg("开始日期和结束日期间隔最多3个月！");
            return false;
        }

        $.fn.jqLoading({ height: 100, width: 240, text: "查询中，请稍等...." });
        chargeBillService.findChargeBill({
            action: 'findChargeBill',
            startDate: $("#startDate").val(),
            endDate: $("#endDate").val(),
            contractUserId: $("#contractUserSel").val()
        }, function (data) {
            $.fn.jqLoading("destroy");
            if (data.status == "SUCCESS") {
                $scope.chargeBillVO = data.data.chargeBillVO;

                //保存列表的查询条件，导出时使用
                $scope.startDateVal = $("#startDate").val();
                $scope.endDateVal = $("#endDate").val();
                $scope.contractUserIdVal = $("#contractUserSel").val();
            } else {
                promptMsg(data.errorReason);
            }
        }, $scope.requestError());
    }

    //选择导出格式
    /*$scope.toChoseFileFormat = function (operate) {
        if(NullOrUndefined($scope.startDateVal) || NullOrUndefined($scope.endDateVal) || NullOrUndefined($scope.contractUserIdVal)){
            promptMsg("请查询后再进行导出！");
            return false;
        }
        $('#choseFileFormatModal').on('show.bs.modal', centerModals("choseFileFormatModal"));
        $("#choseFileFormatModal").modal({
            backdrop: "static",//点击空白处不关闭对话框
            keyboard:false // 按esc键盘不关闭
        });
    }*/

    //打印PDF或者Word文件
    $scope.exportFile = function () {
        if(NullOrUndefined($scope.startDateVal) || NullOrUndefined($scope.endDateVal) || NullOrUndefined($scope.contractUserIdVal)){
            promptMsg("请查询后再进行导出！");
            return false;
        }
        var url = "/chargebill/word?startDate=" + $scope.startDateVal + "&endDate=" + $scope.endDateVal + "&contractUserId=" + $scope.contractUserIdVal;
        window.location.href = url;


        /*if($('input[name="fileFormat"]:checked').val() == "2"){
            window.location.href = "/chargebill/pdf?startDate=" + $scope.startDateVal + "&endDate=" + $scope.endDateVal + "&contractUserId=" + $scope.contractUserIdVal;
        }else{
            window.location.href = "/chargebill/word?startDate=" + $scope.startDateVal + "&endDate=" + $scope.endDateVal + "&contractUserId=" + $scope.contractUserIdVal;
        }
        //关闭modal
        $scope.closeChoseFileFormatModal();*/
    }

    //关闭选择导出格式窗口
    $scope.closeChoseFileFormatModal = function(){
        //恢复默认选项
        $("input:radio[name='fileFormat'][value='1']").prop('checked','checked');
        $("#choseFileFormatModal").modal("hide");
    };

    //打印
    $scope.printFile = function () {
        if(NullOrUndefined($scope.startDateVal) || NullOrUndefined($scope.endDateVal) || NullOrUndefined($scope.contractUserIdVal)){
            promptMsg("请查询后再进行打印！");
            return false;
        }
        // window.location.href = "/chargebill/print?startDate=" + $scope.startDateVal + "&endDate=" + $scope.endDateVal + "&contractUserId=" + $scope.contractUserIdVal;
        document.getElementById("otherprint").style.display = "none";
        var printData = document.getElementById("printpart").innerHTML; //获得 div 里的所有 html 数据
        document.getElementById("printshow").innerHTML = printData;
        window.print();
        //打印结束后，放开隐藏内容
        document.getElementById("otherprint").style.display = "block";
        document.getElementById("printshow").innerHTML = "";
    }
});